package com.lelouch.wetlandaccount.controller;

import com.lelouch.wetlandaccount.service.UserEntityService;
import com.lelouch.wetlandcommon.domain.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统用户控制器
 *
 * @author lcy
 * @date 20180807
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserEntityController {


    @Autowired
    private DiscoveryClient discoveryClient;


    @Autowired
    private UserEntityService userEntityService;


    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String index() {
        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("wetlandsystem");
        for (ServiceInstance serviceInstance : serviceInstances) {
            log.info("host:"+serviceInstance.getHost()+",serviceId:"+serviceInstance.getServiceId());
        }
        return "host:"+serviceInstances.get(0).getHost()+",serviceId:"+serviceInstances.get(0).getServiceId();

    }


    @RequestMapping("/add")
    public UserEntity add() {
        UserEntity userEntity = new UserEntity();
        //userEntity.setId("1");
        userEntity.setUserName("lelouch");
        userEntity.setNo(1L);
        return userEntityService.add(userEntity);

    }

    @RequestMapping("/delete")
    public boolean delete(String id) {
        return userEntityService.delete(id);
    }

    /**
     * update的时候带上id，save方法在没有id的时候默认添加，有id的时候是更新
     *
     * @return
     */
    @RequestMapping("/update")
    public UserEntity update() {
        UserEntity userEntity = new UserEntity();
        userEntity.setId("5b8e4afd9f2e1c0900767afc");
        userEntity.setUserName("lelouch");
        userEntity.setNo(2L);
        userEntity.setEmail("565862329@qq.com");
        return userEntityService.update(userEntity);
    }

    @RequestMapping("/findById")
    public UserEntity findById(String id) {
        return userEntityService.findById(id);
    }

    @RequestMapping("/findByUsername")
    public UserEntity findByUsername(@RequestParam("username") String username) {
        return userEntityService.findByUsername(username);
    }


    //@RequestMapping("/addTest")
    //public TestEntity addTest(){
    //   TestEntity testEntity =new TestEntity();
    //    //userEntity.setId("1");
    //    testEntity.setName("test");
    //    return testEntityService.add(testEntity);
    //
    //}


}
